<extend name="Public/base"/>

<block name="body">
    <div class="main-title">
        <h2>
            修改活动商品
        </h2>
    </div>
    <div class="tab-wrap">
        <div class="tab-content">
            <form id="form" action="{:U('')}" method="post" class="form-horizontal">
                <!--TAB S-->
                <div class="form-item cf">
                    <label class="item-label">所属活动</label>
                    <div class="controls">
                        <input type="text" class="text input-large" name="skname" value="{$skname}" readonly='true'>
                        <input type="hidden" name="skid" value="{$skid}">
                    </div>
                </div>
                <div class="form-item cf">
                    <label class="item-label">关联活动商品<span class="check-tips"><a href='#' class='btn btn-primary' id="addspec">添加新商品</a></span></label>
                    <div class="controls">
                        <div style="border: #000 1px solid;width:80%" class='data-table table-striped'>
                            <table >
                                <thead>
                                    <tr>
                                        <th>商品</th>
                                        <th>活动价格</th>
                                        <th>活动库存</th>
                                        <th>单用户限购量</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody id="tbodyspec">
                                <volist name="data.seckpro" id="vo">
                                    <tr id="spec_{$vo.id}">
                                        <td><input type="text" class="text input-large" readonly="true" name="specid" value="{$vo.pname}({$vo.specname})"></td>
                                        <td><input type="text" class="text input-small" name="price" value="{$vo.price}" placeholder="输入活动价格" ></td>
                                        <td><input type="text" class="text input-small" name="store" value="{$vo.inventory}" placeholder="输入活动库存" ></td>
                                        <td><input type="text" class="text input-small" name="maxcnt" value="{$vo.maxCnt}" placeholder="输入限购数量" ></td>
                                        <td><a href='#' class='btn btn-primary specmodifyajax' data-skid="{$vo.skid}" data-id="{$vo.id}" >修改</a> 
                                            <a href='#' class='btn btn-primary specdelajax' data-skid="{$vo.skid}" data-id="{$vo.id}">删除</a></td>
                                    </tr>
                                </volist>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="form-item cf">
                    <button class="btn submit-btn  ajax-post" id="submit" type="submit" target-form="form-horizontal" autocomplete="off">保存商品</button>
                    <button class="btn btn-return" onclick="javascript:history.back(-1);
                            return false;">返 回</button>
                </div>
            </form>
        </div>
    </div>
</block>

<block name="script">
    <script type="text/javascript">
        $(document).ready(function () {
            $(".tab-nav > li").click(function () {
                var tab = $(this).data('tab');
                $(".tab-nav > li").removeClass('current');
                $(this).addClass('current');
                $(".tab-pane").removeClass('in');
                var obj = $('#' + tab);
                obj.addClass('in');
            });
            ///
            $('#addspec').click(function () {
                var html = '<tr><td>选择商品 <select data-form="select2" style="height: 30px; width:200px;" name="specid[]" ><option value="0">请选择...</option><volist name="data.prolist" id="pl"><option value={$pl.specid},{$pl.pid}>{$pl.pname}({$pl.specname})</option></volist></select></td><td><input type="text" class="text input-small" name="price[]" value="" placeholder="输入活动价格" ></td><td><input type="text" class="text input-small" name="store[]" value="" placeholder="输入活动库存" ></td><td><input type="text" class="text input-small" name="maxcnt[]" value="" placeholder="输入限购数量" ></td><td><a href="#" class="btn btn-primary trdelspec">删除</a></td></tr> ';
                $('#tbodyspec').append(html);
                $('[data-form=select2]').select2();
                $('.trdelspec').bind("click", function () {
                    $(this).parent().parent().remove();
                });
            });


            ///
            $('.specmodifyajax').click(function () {
                var id = $(this).data('id');
                var skid = $(this).data('skid');
                var specarr = Array();
                $(this).parents("tr").find('td input').each(function () {
                    specarr[$(this).attr('name')] = $(this).val();
                });

//                $(this).parents("tr").find('td input[type=checkbox]').each(function () {
//                    var checkbloean = $(this).prop('checked');
//                    if (checkbloean) {
//                        specarr[$(this).attr('name')] = 1;
//                    } else {
//                        specarr[$(this).attr('name')] = 0;
//                    }
//
//
//                });

                $.post("{:U('Seckill/speceditajax')}", {id: id,skid:skid, price: specarr['price'], store: specarr['store'], maxcnt: specarr['maxcnt']},
                function (data) {
                    alert(data.info);
                }, "json");

            });

            $('.specdelajax').click(function () {
                if (window.confirm("请仔细核确认，需要删除此规格吗？")) {
                    var id = $(this).data('id');
                    var skid = $(this).data('skid');
                    var THIS = $(this).parent().parent();
                    $.post("{:U('Seckill/specdelajax')}", {id: id,skid:skid},
                    function (data) {
                        alert(data.info);
                        THIS.remove();
                    }, "json");
                }
            });
        });
        highlight_subnav('{:U('Seckill/proindex')}');
    </script>
</block>